﻿using System;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace QDCommon.Secret
{
    public class Base64Helper
    {
        public Base64Helper()
        {
        }

        public static string EncodeBase64(byte[] data)
        {
            string result = null;
            try
            {
                result = Convert.ToBase64String(data);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            return result;
        }
        public static string EncodeBase64(string str,Encoding encoding)
        {
            var bytes = encoding.GetBytes(str);
            return EncodeBase64(bytes);
        }

        public static async Task<string> EncodeBase64(Stream stream)
        {
            var length = stream.Length;
            byte[] bytes = new byte[length];
            var count = await stream.ReadAsync(bytes, 0, (int)length);
            return EncodeBase64(bytes);
        }
        public static byte[] DecodeBase64(string base64Str)
        {
            byte[] bytes = Convert.FromBase64String(base64Str);
            return bytes;
        }
    }
}
